#!/bin/bash

# GitCode Issue Feedback 插件演示启动脚本

echo "🚀 启动 GitCode Issue Feedback 插件演示..."
echo ""

# 检查 Node.js 版本
echo "📋 检查环境..."
node_version=$(node -v 2>/dev/null)
if [ $? -eq 0 ]; then
    echo "✅ Node.js 版本: $node_version"
else
    echo "❌ 未找到 Node.js，请先安装 Node.js"
    exit 1
fi

# 检查 npm
npm_version=$(npm -v 2>/dev/null)
if [ $? -eq 0 ]; then
    echo "✅ npm 版本: $npm_version"
else
    echo "❌ 未找到 npm"
    exit 1
fi

# 检查 GitBook CLI
gitbook_version=$(gitbook -V 2>/dev/null)
if [ $? -eq 0 ]; then
    echo "✅ GitBook CLI 版本: $gitbook_version"
else
    echo "⚠️  未找到 GitBook CLI，正在安装..."
    npm install -g gitbook-cli
fi

echo ""

# 安装依赖
echo "📦 安装依赖..."
npm install

if [ $? -eq 0 ]; then
    echo "✅ 依赖安装成功"
else
    echo "❌ 依赖安装失败"
    exit 1
fi

echo ""

# 构建插件
echo "🔨 构建插件..."
cd ..
npm run build
cd example

if [ $? -eq 0 ]; then
    echo "✅ 插件构建成功"
else
    echo "❌ 插件构建失败"
    exit 1
fi

echo ""

# 启动 GitBook 服务器
echo "🌐 启动 GitBook 服务器..."
echo "📍 服务器地址: http://localhost:4000"
echo "📍 按 Ctrl+C 停止服务器"
echo ""

gitbook serve --port 4000
